#include "ReportExport.h"
#include "OfficeDocment.h"
#include "QFile"
#include "qapplication.h"
#include "QDir"
#include "QFontMetrics"
#include "QPixmap"
#define DATA_TEMP_DIR tr("DataTmep")

ReportExport::ReportExport(QObject *parent):
    QObject(parent),
    m_pFile(nullptr),
    m_pOfficeDoc(nullptr)
{

    m_nImgIndex =0;
    m_pOfficeDoc = new OfficeDocment(this);
}



ReportExport::ReportExport(const QString &fileName, QObject *parent):
    QObject(parent),
    m_pFile(nullptr),
    m_pOfficeDoc(nullptr)
{
    m_nImgIndex =0;
    m_pOfficeDoc = new OfficeDocment(fileName,this);
    checkAndCreatTmepPath();
}
void ReportExport::setReportPath(const QString &fileName)
{
    if(m_pOfficeDoc)
    {
        if(m_pFile)
        {
            m_pFile->close();
            m_pFile = new QFile(fileName);
        }
        m_pOfficeDoc->setFilePath(m_pFile);
    }
}

void ReportExport::appendParagraph(const QString &contex, const ElementStytle &eleStyle)
{
    HtmlElement * ele = m_pOfficeDoc->creactEle(EleFactory::P);
    if(ele)
    {
        setEleStytle(ele,eleStyle);
        ele->setContent(contex);
        m_pOfficeDoc->appendElement(ele);
    }
}

void ReportExport::appendImage(const QString &imgFile, int imgW, int imgH, const QString &text,
                               ReportExport::AlignType type, const ElementStytle &TextEleStyle)
{
    HtmlElement * div =  (m_pOfficeDoc->creactEle(EleFactory::Div));
    if(div)
    {
        div->alignCenter();
        QFont font(TextEleStyle);
        QFontMetrics fontMetrics(font);

        div->setEleHeight(imgW +fontMetrics.height()+10);

    }



    HtmlElement *img = m_pOfficeDoc->creactEle(EleFactory::Img);
    if(img)
    {
        img->setContent(imgFile);
        img->setEleHeight(imgH);
        img->setEleWidth(imgW);
    }
    HtmlElement * ele = m_pOfficeDoc->creactEle(EleFactory::P);
    if(ele)
    {
        setEleStytle(ele,TextEleStyle);
        switch (type) {
        case Center:
            ele->alignCenter();
            break;
        case Left:
            ele->alignLeft();
            break;
        case Right:
            ele->alignRight();
            break;
        default:
            break;
        }
        ele->setContent(text);

    }

    if(div && img && ele)
    {
        div->appendElement(img);
        div->appendElement(ele);
        m_pOfficeDoc->appendElement(div);
    }
    else
    {
        if(img)
        {
            img->deleteLater();
            img = nullptr;
        }

        if(div)
        {
            div->deleteLater();
            div = nullptr;
        }

        if(ele)
        {
            ele->deleteLater();
            ele = nullptr;
        }
    }

}

void ReportExport::appendImage( QObject *widget, int imgW, int imgH, const QString &text,
                                ReportExport::AlignType type, const ElementStytle &TextEleStyle)
{
    // 进行截图
    QPixmap screenshot = QPixmap::grabWidget(widget);

    QString filePath = m_strTempDataFilePath + QDir::separator()+ QString("img_%1.png").arg(m_nImgIndex++);

    screenshot.save(filePath);

    appendImage(filePath,imgW,imgH,text,type,TextEleStyle);
}

void ReportExport::appendTable(const TabHeader &header, const TabData &data, const QString &text, ReportExport::AlignType type, const ElementStytle &TextEleStyle)
{

    HtmlElement *table = m_pOfficeDoc->creactEle(EleFactory::Table);
    if(table)
    {
        table->setStyleSheet("border", "1px solid black");
        //遍历表头
        HtmlElement *tr = m_pOfficeDoc->creactEle(EleFactory::Tr);
        if(tr)
        {
            foreach (const QString &tdText, header.headerData)
            {
                HtmlElement *td = m_pOfficeDoc->creactEle(EleFactory::Td);
                if(td)
                {
                    td->setStyleSheet("border", "1px solid black");
                    td->setContent(tdText);
                    setEleStytle(td,header.headerStytle);
                    tr->appendElement(td);
                    qDebug()<<td->styleSheet();
                }

            }
            table->appendElement(tr);
        }
        tr = nullptr;
        //遍历表数据
        foreach (const QList<QString> &trcont, data.tableData)
        {
            tr = m_pOfficeDoc->creactEle(EleFactory::Tr);
            if(tr)
            {
                foreach (const QString &tdText, trcont)
                {
                    HtmlElement *td = m_pOfficeDoc->creactEle(EleFactory::Td);
                    if(td)
                    {
                        td->setStyleSheet("border", "1px solid black");
                        td->setContent(tdText);
                        setEleStytle(td,header.headerStytle);
                        tr->appendElement(td);
                    }

                }
            }
            table->appendElement(tr);
        }
        m_pOfficeDoc->appendElement(table);
    }
}

void ReportExport::exportReportPdf()
{
    m_pOfficeDoc->printToFile(OfficeDocment::pdf);
}

void ReportExport::exportReportExcel()
{
    m_pOfficeDoc->printToFile(OfficeDocment::xls);
}

void ReportExport::exportReportWord()
{
    m_pOfficeDoc->printToFile(OfficeDocment::doc);
}


void ReportExport::checkAndCreatTmepPath()
{
    QString appCurrentApp = QApplication::applicationDirPath();

    QDir info(appCurrentApp+QDir::separator()+DATA_TEMP_DIR);
    if(!info.exists())
    {
        qDebug()<<info.mkdir(".");
    }
    qDebug()<<info.absolutePath();
    m_strTempDataFilePath = info.absolutePath();
}

void ReportExport::setEleStytle(HtmlElement *ele, ElementStytle stytle)
{
    if(ele)
    {
        ele->setRgb(stytle.rgb());
        ele->setBold(stytle.bold());
        ele->setFamily(stytle.family());
        ele->setItalic(stytle.italic());
        ele->setPixelSize(stytle.pixelSize());
    }
}
